﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption
Partial Class NewLaunch
    Inherits System.Web.UI.Page
#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function
    Protected Sub lnkPrice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPrice.Click
        If lnkPrice.Text = "Price▼" Then
            lnkPrice.Text = "Price▲"
        Else
            lnkPrice.Text = "Price▼"
        End If

        lblhidSortType.Text = " ORDER BY ProjectPrice " & SortType(lnkPrice.Text)
                             

        btnSearch_Click(sender, e)
    End Sub


    Protected Sub lnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkName.Click
        If lnkName.Text = "Project Name▼" Then
            lnkName.Text = "Project Name▲"
        Else
            lnkName.Text = "Project Name▼"
        End If

        lblhidSortType.Text = " ORDER BY ProjectName " & SortType(lnkName.Text)


        btnSearch_Click(sender, e)
    End Sub


    Protected Sub lnkDescription_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDescription.Click
        If lnkDescription.Text = "Project Description▼" Then
            lnkDescription.Text = "Project Description▲"
        Else
            lnkDescription.Text = "Project Description▼"
        End If

        lblhidSortType.Text = " ORDER BY CAST(a.[ProjectDetails] AS VARCHAR(500)) " & SortType(lnkDescription.Text)


        btnSearch_Click(sender, e)
    End Sub


    Protected Sub lnkLocation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLocation.Click
        If lnkLocation.Text = "Project Location▼" Then
            lnkLocation.Text = "Project Location▲"
        Else
            lnkLocation.Text = "Project Location▼"
        End If

        lblhidSortType.Text = " ORDER BY ProjectLocation " & SortType(lnkLocation.Text)


        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkWebsite_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkWebsite.Click
        If lnkWebsite.Text = "Website▼" Then
            lnkWebsite.Text = "Website▲"
        Else
            lnkWebsite.Text = "Website▼"
        End If

        lblhidSortType.Text = " ORDER BY Website " & SortType(lnkWebsite.Text)


        btnSearch_Click(sender, e)
    End Sub

    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        divError.Visible = False
        Try
            objDB.OpenConnection()

            Dim ProjectName, Location, Description, PriceMin, PriceMax, Website As String

            If txtProjectName.Text <> String.Empty Then
                ProjectName = " AND ProjectName LIKE @ProjectName"
                objDB.AddParameter("ProjectName", Data.SqlDbType.NVarChar, "%" & txtProjectName.Text.Trim & "%")
            Else
                ProjectName = String.Empty
            End If

            If txtLocation.Text <> String.Empty Then
                Location = " AND ProjectLocation LIKE @ProjectLocation"
                '" AND Alamat LIKE '%" + txtAlamat.Text.Replace(",", "%' AND [Title] LIKE '%") + "%'"
                objDB.AddParameter("ProjectLocation", Data.SqlDbType.NVarChar, "%" & txtLocation.Text.Trim & "%")
            Else
                Location = String.Empty
            End If

            If txtDescription.Text <> String.Empty Then
                Description = " AND ProjectDetails LIKE @ProjectDetails"
                objDB.AddParameter("ProjectDetails", Data.SqlDbType.NVarChar, "%" & txtDescription.Text.Trim & "%")
            Else
                Description = String.Empty
            End If


            'If txtPriceMin.Text.Trim <> String.Empty Then
            '    PriceMin = " AND ProjectPrice >= @PriceMin"
            '    objDB.AddParameter("PriceMin", Data.SqlDbType.Decimal, txtPriceMin.Text.Trim)
            'Else
            '    PriceMin = String.Empty
            'End If

            'If txtPriceMax.Text.Trim <> String.Empty Then
            '    PriceMax = " AND ProjectPrice <= @PriceMax"
            '    objDB.AddParameter("PriceMax", Data.SqlDbType.Decimal, txtPriceMax.Text.Trim)
            'Else
            '    PriceMax = String.Empty
            'End If

            If txtWebsite.Text <> String.Empty Then
                Website = " AND Website LIKE @Website"
                objDB.AddParameter("Website", Data.SqlDbType.NVarChar, "%" & txtWebsite.Text.Trim & "%")
            Else
                Website = String.Empty
            End If

            objDB.dbVariable.SQLString = "SELECT a.ProjectId, a.ProjectName, a.ProjectDetails, a.ProjectLocation,a.Website,a.ProjectPrice" & _
                                        " FROM Mobile_NewLaunch a " & _
                                        "WHERE a.StatusDelete <> 1 " &
                                        ProjectName & Description & Location & Website & PriceMin & PriceMax &
                                        lblhidSortType.Text

            objDB.FillData(TempDT, "Mobile_NewLaunch", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repUpload.DataSource = objPaged
            repUpload.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount

        Catch ex As Exception
            divError.Visible = True
            lblMsgError.Text = ex.Message.ToString
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub


    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            '1-Back Page
            '2-Page Size
            '3-Current Page

            Dim strBack As String
            strBack = "NewLaunch.aspx" & "ڡ" & PageSize & "ڡ" & CurrentPage & "ڡ" & txtProjectName.Text.Trim & "ڡ" & _
                    txtDescription.Text.Trim & "ڡ" & txtLocation.Text.Trim & "ڡ" & txtWebsite.Text.Trim
            Session.Add("Back", "NewLaunch.aspx")
            'Response.Redirect("AuctionEntry.aspx?id=" & SymmetricEncryptToHex(e.CommandArgument, "augment"))
            Response.Redirect("NewLaunchEntry.aspx?id=" & e.CommandArgument)
        End If

        If e.CommandName.ToUpper = "DELETEITEM" Then

            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE Mobile_NewLaunch SET StatusDelete=1 WHERE ProjectId=@ProjectId"
                objDB.AddParameter("ProjectId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repUpload.Items(e.CommandArgument).FindControl("lblProjectId"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Entry Deleted"
                lblMsgSuccessful.Visible = True
                LoadUpload()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '1-Back Page
            '2-Page Size
            '3-Current Page
            '4-UnitNo
            '5-Address
            '6-Type
            '7-Venue
            '8-Bank
            '9-Trkh Penilaian
            '10-Price
            '11-Auction Date
            '12-Alamat
            '13-Agent

            If arr(0).ToString.Contains("NewLaunch.aspx") Then
                'PageSize = CInt(arr(1).ToString)
                'CurrentPage = CInt(arr(2).ToString)
                'txtProjectName.Text = arr(3).ToString
                'txtDescription.Text = arr(4).ToString
                'txtLocation.Text = arr(5).ToString

                ''txtPriceMin.Text = arr(6).ToString
                ''txtPriceMax.Text = arr(7).ToString
                'txtWebsite.Text = arr(6).ToString

                Session.Remove("Back")
                LoadUpload()

            Else
                Session.Remove("Back")
            End If
        Else
            If Not Page.IsPostBack Then

                lblStaffUID.Text = Session.Item("StaffId").ToString
                lblStaffType.Text = Session.Item("StaffType").ToString
                lnkPrice_Click(sender, e)
            End If
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub


    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        Session("anchor1") = ""
        LoadUpload()
    End Sub


    Protected Sub btnResetSearchFilter_Click(sender As Object, e As EventArgs) Handles btnResetSearchFilter.Click
        txtDescription.Text = ""
        txtLocation.Text = ""
        txtProjectName.Text = ""
        txtWebsite.Text = ""

        'txtPriceMin.Text = ""
        'txtPriceMax.Text = ""

    End Sub

End Class
